﻿using Microsoft.Reporting.WinForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplicationPlatform
{
    public partial class frmPrintReport : Form
    {
        private IList<Stream> m_streams;
        private int m_currentPageIndex;

        public frmPrintReport()
        {
            InitializeComponent();
        }


        private void Print()
        {
            PrintDocument printDoc = new PrintDocument();

            if (m_streams == null || m_streams.Count == 0)
                return;

            string printer = "Microsoft Print to PDF"; //打印机名称

            printDoc.PrinterSettings.PrinterName = printer;
            PrintController pc = new StandardPrintController();
            printDoc.PrintController = pc;

            printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
            printDoc.Print();
        }


        private void PrintPage(object sender, PrintPageEventArgs ev)
        {
            Metafile pageImage = new
               Metafile(m_streams[m_currentPageIndex]);
            ev.Graphics.DrawImage(pageImage, ev.PageBounds);
            m_currentPageIndex++;
            ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
        }

        private DataTable cratePrintData()
        {
            DataTable rdt = new DataTable();

            rdt.Columns.Add("Id");
            rdt.Columns.Add("Name");

            DataRow inDr = rdt.NewRow();
            inDr["id"] = "1";
            inDr["name"] = "张三";

            rdt.Rows.Add(inDr);

            return rdt;
        }

        //导入文件流
        private void ExportStream(LocalReport localReport)
        {
            string deviceInfo =
                  "<DeviceInfo>" +
                  "  <OutputFormat>emf</OutputFormat>" +
                  "  <DpiX>0</DpiX>" +
                  "  <DpiY>0</DpiY>" +
                  "  <PageWidth>14.9cm</PageWidth>" +
                  "  <PageHeight>21cm</PageHeight>" +
                  "  <MarginTop>0in</MarginTop>" +
                  "  <MarginLeft>0in</MarginLeft>" +
                  "  <MarginRight>0in</MarginRight>" +
                  "  <MarginBottom>0in</MarginBottom>" +
                  "</DeviceInfo>";
            Warning[] warnings;
            m_streams = new List<Stream>();
            localReport.Render("Image", deviceInfo, CreateStream, out warnings);
            if (m_streams != null)
            {
                foreach (Stream stream in m_streams)
                {
                    stream.Position = 0;
                    //stream.Close();
                }
                //  m_streams = null;
            }
            localReport.Dispose();
        }

        private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
        {
            //name +
            Stream stream = new FileStream(name +
               "1." + fileNameExtension, FileMode.Create);
            m_streams.Add(stream);

            return stream;
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            //生成打印数据
            DataTable pdt = cratePrintData();

            LocalReport report = new LocalReport();

            //获取报表
            report.ReportPath = Application.StartupPath + "\\Reports\\ReportTest.rdlc";
            report.DataSources.Add(new ReportDataSource("DataSetTest_DataTableTest", pdt)); //DataSetTest_DataTableTest 很重要必须与数据源一致

            ExportStream(report);

            m_currentPageIndex = 0;
            //打印操作
            Print();
        }

    }
}
